package com.seagull.sb.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.seagull.sb.dao.AuthenticationDAO;
import com.seagull.sb.domain.Student;
import com.seagull.sb.domain.Teacher;

public class AuthenticationDAOImpl extends BaseDAO implements AuthenticationDAO {

	@Override
	public Student findStudent(String username, String password)
			throws Exception {
		Map<String,Object> qmap = new HashMap<String, Object>();
		List<Student> students;
		qmap.put("username", new Long(username)); // dob
		qmap.put("password", new Long(password)); // student id
		Object result = sqlMap.queryForList("studentDB.getStudentByToken",qmap);
		if(result instanceof List){
			students = (List<Student>)result; 
			if (students.size() == 1){
				return students.get(0); 
			}else if (students.size() > 1){
				throw new Exception("Too many students with similiar token number");
			}else if (students.size() < 1){
				throw new Exception("No record found with given token.");
			}
		}else{
			throw new Exception("No record found with given token.");
		}
		return new Student();
	}
		
	
	@Override
	public Teacher findTeacher(StringBuffer username, String password)
			throws Exception {
		Map<String,Object> qmap = new HashMap<String, Object>();
		List<Teacher> teachers;
		
		qmap.put("email", username.toString().trim().toLowerCase());
		qmap.put("pwd", password.trim());
		Object result = sqlMap.queryForList("organizationDB.getTeacherByToken",qmap);
		if(result instanceof List){
			teachers = (List<Teacher>)result; 
			if (teachers.size() == 1){
				return teachers.get(0); 
			}else if (teachers.size() > 1){
				throw new Exception("Too many teacher with similiar token number");
			}else if (teachers.size() < 1){
				throw new Exception("No record found with given token.");
			}
		}else{
			throw new Exception("No record found with given token.");
		}
		return new Teacher();
	}	
	
}
